<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%
	request.setAttribute("title","产品参数");
	request.setAttribute("modelConfigPath", "cargo");
%>

<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style type="text/css">
.hot-container{
overflow: auto;
-webkit-box-shadow: 0 0 0 0; 
 box-shadow: 0 0 0 0;
}

.colHeader{
word-wrap:break-word;white-space:normal
}
td{
word-break:break-all;
}
.handsontable .htCore{
	
}
.panel-body1 {
    padding: 0px !important;
}
</style>

<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
	<t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="saveForm()" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>

	<div class="panel-body1">
		<div class="hot-container">
			<div id="hot_parameter_info"></div>
		</div>
	</div>
</t:panel>

<script type="text/javascript">
	function createSpreadsheetData(rowCount, colCount) {
		rowCount = typeof rowCount === 'number' ? rowCount : 100;
		colCount = typeof colCount === 'number' ? colCount : 4;
		var rows = [], i, j;
		for (i = 0; i < rowCount; i++) {
			var row = [];
			for (j = 0; j < colCount; j++) {
				row.push('');
			}
			rows.push(row);
		}
		return rows;
	}
	var $hot=null;
	$(function() {
		initTable();
		$(".panel-body1").height($(window).height()-70);
		$(".wtHolder").height($(window).height());
		reload();
		setTimeout(function(){
			$(".panel-body1").click();	
		},1000);
	});
	
	var empty=function(value, callback){
		setTimeout(function(){
			callback(!$.isNull(value));	
		},10);
	};
	
	
	function initTable(){
		
		var hot_parameter_info = document.getElementById('hot_parameter_info');
		$hot = new Handsontable(hot_parameter_info, {
			rowHeaders : true,
			autoColumnSize:true,
		
			stretchH:'last',
			contextMenu: ['row_above', 'row_below', 'remove_row'],
			colHeaders : [ '客户姓名', '联系方式', '备注'],
			colWidths:[100,200],
			columns: [{data:'customerName',validator:empty},
			           {data:'customerTel'},
			           {data:'comment'}]
		});

		$hot.updateSettings({
			contextMenu : {
				items : {
					"row_above" : {name : '在上面添加行'},
					"row_below" : {name : '在下面添加行'},
					"remove_row" : {name : '删除当前行'}
				}
			}
		});
	}
	
	
	function loadTable(){
		return getEmptyData();
	
	}
	
	function getEmptyData() {
		
		var hot_borders_data = createSpreadsheetData(10, 8);
		return hot_borders_data;
	}
	
	function reload(){
		var data=loadTable();
		$hot.loadData(data);
		$hot.render();
	}
	
	function saveForm(){
		//var productVersionId=$("#productVersionId").val();
		deleteEmpty();
		
		$hot.validateCells(function(result){
			if(!result){
				$.topNotify("表格有错误信息，检查表格红色区域","error");
				return;
			}
			var parameterInfos=getDataJson();
			var productNoJson=null;
			//var productNos=getProductNoJson();
			
			if($.isNull(parameterInfos)){
				$.topNotify("表格无内容","error");
				return;
			}
			//var urltest=$.getVirtualPath()+"/admin/shop/customer/validate?productNos="+productNos;
			//var str=$.getTextPost(urltest); 
			//productNoJson =JSON.parse(str);
			var url=$.getVirtualPath()+"/shop/customer/saves";
			$.ajax({
				url:url,
				type:'post',
				dataType:'json',
				contentType:"application/json",
				data:JSON.stringify(parameterInfos),
				success:function(result){
					top.main.formSubmit();
					top.$("#modal_customer_form").modal("hide");
					$.topNotify(result.message,result.resultType);	
				}
			});
			
		});
		
	}
	
	/* function getProductNoJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push(data.customerName);
		}
		
		return columns.join(",");
		
	} */
	
	function getDataJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push({"customerName":data.customerName,
						  "customerTel":data.customerTel,
						  "comment":data.comment
						  });
		}
		return columns;
	}
	
	//删除空行
	function deleteEmpty(){
		var rows=$hot.countRows();
		for(var i=rows-1;i>=0;i--){
			if($hot.isEmptyRow(i)){
				$hot.alter('remove_row', i);
			}
		}
	}
	
</script>

<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/handsontable.jsp"%>
<%@ include file="/WEB-INF/views/shared/scripts/editor/ueditor.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>

<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>